package sky.springboot.springbootjpa.bean;

import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

// EntityListeners => 开始使用jpa的四个注解的使用
@Entity
@EntityListeners(AuditingEntityListener.class)
public class User implements Serializable{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false, unique = true)
    private String userName;

    @Column(nullable = false)
    private String password;

    @Column(nullable = false)
    private int age;

    @CreatedBy
    private String createuser;

    @CreatedDate
    private Date createtime;

    @LastModifiedDate
    private Date updatetime;

    @LastModifiedBy
    private String updateuser;


    // o2o
    /*@OneToOne(cascade = CascadeType.ALL)
    // 形成对role的外检关联 JoinColumn维护外检
    @JoinColumn(name = "role_id")
    private Role role;
    */

    // o2m
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "role_id")
    private Role role;

    public Role getRole() {
        return role;
    }
    public void setRole(Role role) {
        this.role = role;
    }

    public User() {
    }

    public User(String userName, String password, int age) {
        this.userName = userName;
        this.password = password;
        this.age = age;
    }

    public Long getId() {
        return id;
    }

    public String getUserName() {
        return userName;
    }

    public String getPassword() {
        return password;
    }

    public int getAge() {
        return age;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setAge(int age) {
        this.age = age;
    }


    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                ", age=" + age +
                ", createuser='" + createuser + '\'' +
                ", createtime=" + createtime +
                ", updatetime=" + updatetime +
                ", updateuser='" + updateuser + '\'' +
                ", role=" + role +
                '}';
    }
}
